<link  href="__PUBLIC__/lib/webuploader/css/webuploader.css" rel="stylesheet" type="text/css">
<style>
    .modal-dialog{
        width: 400px;;
    }
    .webuploader-pick {
        border:0;
        padding:0;
        border-radius: 0;
    }
</style>
<div class="clearfix">
    <div class="col-xs-12">

        <input type="hidden" name="user_cover" id="user_cover" value="{$user_info['cover_id']}"/>
        <div style="color: #999;">
            {:L('_PIC_SUGGEST_')}
        </div>
        <div class="upload-background-box" style="margin-top: 10px;width: 300px;height: 80px;">
            <if condition="$my_cover['cover_id']">
                <div class="upload-pre-item" style="width: 348px;height:70px;"><img class="img-responsive" style="width: 100%;height:100%;" src="{$my_cover.cover_path}"/></div>
            </if>
            <div class="progress-box"></div>
        </div>
        <div class="clearfix"></div>
 
        <div id="imgPicker" class="btn btn-primary">{:L('_COVER_UPLOAD_')}</div>
        <if condition="$my_cover['cover_id']">
            <div class="btn btn-primary pull-right" id="submit_cover" disabled="disabled" style="width: 100px;">{:L('_SAVE_')}</div>
        <else/>
            <div class="btn btn-primary pull-right" id="submit_cover" disabled="disabled" style="width: 100px;display: none;">{:L('_SAVE_')}</div>
        </if>

    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/lib/webuploader/js/webuploader.js"></script>

<script>
    $(function(){

        //上传图片
         // 初始化Web Uploader
        var uploader = WebUploader.create({
            // 选完文件后，是否自动上传。
            auto: true,
            // swf文件路径
            swf: '__PUBLIC__/lib/WebUploader/js/Uploader.swf',
            // 文件接收服务端。
            server: "{:U('Home/File/uploadPicture',array('session_id'=>session_id()))}",
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: '#imgPicker',
            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/jpg,image/jpeg,image/png'
            }
        });

        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            uploader.upload();
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $percent = $('.upload-progress');

            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<div class="progress"><div class="progress-bar upload-progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%"><span class="sr-only">0%</span></div></div>')
                        .appendTo('.progress-box' )
                        .find('.upload-progress');
            }
            $percent.find('span').html(percentage * 100 + '%');
            $percent.css( 'width', percentage * 100 + '%' );
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on( 'uploadSuccess', function( file,data ) {
            var data = data.data;
            $('#user_cover').val(data.file.id);
            $('.progress').remove(); //移除进度条
            $('.upload-background-box').html('<div class="upload-pre-item" style="width: 348px;height:70px;"><img class="img-responsive" style="width: 100%;height:100%;" src="'+data.file.path+'"/></div>');
            $('#submit_cover').attr('disabled',false);
            $('#submit_cover').show();
        });

        // 文件上传失败，显示上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                $error = $li.find('div.error');

            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
            $('#submit_cover').hide();
            toast.error("{:L('_SUCCESS_COVER_SELECT_')}{:L('_PERIOD_')}", "{:L('_TIP_GENTLE_')}");
            $error.text('上传失败');
        });
        //保存
        $('#submit_cover').click(function () {
            var cover_id = $('#user_cover').val();
            $.post(U('Ucenter/Public/changeCover'), {cover_id: cover_id}, function (msg) {
                if (msg.status == 1) {
                    $('top_bg_box').css('backgroundImage','url("'+msg.path_1140+'")');
                    $('.close').click();
                    toast.success("{:L('_SUCCESS_COVER_CHANGE_')}{:L('_EXCLAMATION_')}",);
                    setTimeout(function(){
                        window.location.href=U("Ucenter/index/index");
                    },1500);
                } else {
                    handleAjax(msg)
                }
            });
        });
    });
</script>